package org.jf.tool.fx.jftoolfx.tools.cron.controller;

import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Spinner;
import javafx.scene.control.ToggleGroup;

import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;

/**
 * cron 工具
 *
 * @author jifeng_zhang
 * @date 2025/10/9 上午11:41
 */
public class MonthController extends CronController {

    @FXML
    private ToggleGroup typeToggleGroup;
    @FXML
    private Spinner<Integer> periodBegin;
    @FXML
    private Spinner<Integer> periodEnd;
    @FXML
    private Spinner<Integer> startBegin;
    @FXML
    private Spinner<Integer> startEnd;
    @FXML
    private CheckBox checkBox1;
    @FXML
    private CheckBox checkBox2;
    @FXML
    private CheckBox checkBox3;
    @FXML
    private CheckBox checkBox4;
    @FXML
    private CheckBox checkBox5;
    @FXML
    private CheckBox checkBox6;
    @FXML
    private CheckBox checkBox7;
    @FXML
    private CheckBox checkBox8;
    @FXML
    private CheckBox checkBox9;
    @FXML
    private CheckBox checkBox10;
    @FXML
    private CheckBox checkBox11;
    @FXML
    private CheckBox checkBox12;

    private List<CheckBox> allCheckBoxes;
    private CronController cronController;

    @Override
    protected void init(URL url, ResourceBundle resourceBundle) {
        allCheckBoxes = Arrays.asList(checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6, checkBox7, checkBox8, checkBox9, checkBox10, checkBox11, checkBox12);
        // 添加值改变监听器（推荐方式）
        periodBegin.valueProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue >= periodEnd.getValue()) {
                periodBegin.getValueFactory().setValue(periodEnd.getValue() - 1);
            }
            typeCheck();
        });
        periodEnd.valueProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue <= periodBegin.getValue()) {
                periodEnd.getValueFactory().setValue(periodBegin.getValue() + 1);
            }
            typeCheck();
        });
        startBegin.valueProperty().addListener((observable, oldValue, newValue) -> {
            typeCheck();
        });
        startEnd.valueProperty().addListener((observable, oldValue, newValue) -> {
            typeCheck();
        });
    }

    public void setCronController(CronController cronController) {
        this.cronController = cronController;
    }

    public void typeCheck() {
        String type = typeToggleGroup.getSelectedToggle().getUserData().toString();
        cronController.cronMonthText.setText("*");
        switch (type) {
            case "period":
                int periodBeginValue = periodBegin.getValue();
                int periodEndValue = periodEnd.getValue();
                cronController.cronMonthText.setText(periodBeginValue + "-" + periodEndValue);
                break;
            case "start":
                int startBeginValue = startBegin.getValue();
                int startEndValue = startEnd.getValue();
                cronController.cronMonthText.setText(startBeginValue + "/" + startEndValue);
                break;
            case "select":
                StringBuilder selectText = new StringBuilder();
                for (int i = 0; i < allCheckBoxes.size(); i++) {
                    if (allCheckBoxes.get(i).isSelected()) {
                        selectText.append(i + 1).append(",");
                    }
                }
                if (!selectText.isEmpty()) {
                    selectText = selectText.deleteCharAt(selectText.length() - 1);
                } else {
                    selectText.append("*");
                }
                cronController.cronMonthText.setText(selectText.toString());
                break;
        }
        cronController.cronText.setText(cronController.cronSecondText.getText() + " " +
                cronController.cronMinuteText.getText() + " " +
                cronController.cronHourText.getText() + " " +
                cronController.cronDayText.getText() + " " +
                cronController.cronMonthText.getText() + " " +
                cronController.cronWeekText.getText() + " " +
                cronController.cronYearText.getText());
        cronController.resultText.setText("");
    }
}
